/**
* helper javascript library for prothom alo blog
* @author: Hasin Hayder [countdraculla@gmail.com] and Emran Hasan[phpfour@gmail.com]
* @author: portion of code is written by Anis Uddin Ahmed [anisniit@gmail.com]
*
*/
var activeta = "";

function makeBanglaEditor(id)
{
	setBanglaContextMenu(id);
	makePhoneticEditor(id);
}

function makeBanglaInput(id)
{
	//id is without #
	setBanglaContextMenu(id);
	makePhoneticEditor(id);
}


function setBanglaContextMenu(input)
{
	$("#"+input).contextMenu('myMenu1', {
		menuStyle: {
			width: '100px'
		},
		bindings: {
		'bkb1': function(t) {
			switched=false;
			makePhoneticEditor(input);
			t.focus();
		},
		'bkb2': function(t) {
			switched=false;
			makeProbhatEditor(input);
			t.focus();
		},
		'bkb3': function(t) {
			switched=false;
			makeInscriptEditor(input);
			t.focus();
		},
		'bkb4': function(t) {
			switched=false;
			makeUnijoyEditor(input);
			t.focus();
		},
		'bkb7': function(t) {
			switched=false;
			makeBijoyEditor(input);
			t.focus();
		},
		'bkb5': function(t) {
			switched=true;
			t.focus();
		},
		'bkb6': function(t) {
			var top =$(t).position().top+30;
			var left =$(t).position().left+50;
			t.focus();
			showVk(top, left);
		}
		}
	});
}

/* VK */
var ir, bangla, banglaSafe="";

var reserved = "2445,2446,2449,2450,2473,2481,2483,2484,2485,2444,2500,2501,2502,";
function drawVirtualKeyboard(){
	var counter = 0;
	for(i=2437; i<=2489;i++)
	{
		//
		ir = reserved.indexOf(i+",");

		if (ir=="-1"){
			bangla = "\\u0"+num2hex(i);
			banglaSafe = "&#"+i+";";
			if (i<2453 )
			drawButton(i,"vkvowels");
			else{
				drawButton(i,"vkconsonants");
				counter++;
			}

			//$("<input  type='button' onclick='insertAtCursor(\""+bangla+"\")' value='"+banglaSafe+"' />").appendTo('#consonants');

			if ( i>2453 && counter>=10) {
				$("<br/>").appendTo("#vkconsonants");
				counter=0;
			}
		}
	}

	drawHexButton("\u0995\u09CD\u09B7","vkconsonants");
	drawHexButton("\u09DC","vkconsonants");
	drawHexButton("\u09DD","vkconsonants");
	drawHexButton("\u09DF","vkconsonants");
	drawHexButton("\u09CE","vkconsonants");
	drawHexButton("\u0982","vkconsonants");
	drawHexButton("\u0983","vkconsonants");
	drawHexButton("\u0981","vkconsonants");

	$("#vkvowels input").attr("class","bangla2");
	$("#vkconsonants input").attr("class","bangla1");

	$("<input class='bangla1 space' type='button' onclick='insertAtCursor(\" \")' value='\u09B8\u09CD\u09AA\u09C7\u09B8' />").appendTo('#vkconsonants');
	$("<input class='bangla1 dari' type='button' onclick='insertAtCursor(\"\u0964\")' value='\u0964' />").appendTo('#vkconsonants');
	$("<input class='bangla1 space' type='button' onclick='insertAtCursor(\"\u09CD\")' value='\u09AF\u09C1\u0995\u09CD\u09A4\u0020\u0995\u09B0\u09C1\u09A8' />").appendTo('#vkconsonants');
	$("<br/>").appendTo("#vkconsonants");


	for(i=2534; i<=2543;i++)
	{
		bangla = "\\u0"+num2hex(i);
		banglaSafe = "&#"+i+";";
		$("<input  type='button' onclick='insertAtCursor(\""+bangla+"\")' value='"+banglaSafe+"' />").appendTo('#vknumbers');
	}

	for(i=2494; i<=2504;i++)
	{
		ir = reserved.indexOf(i+",");

		if (ir=="-1"){
			bangla = "\\u0"+num2hex(i);
			banglaSafe = "&#"+i+";";
			$("<input  type='button' onclick='insertAtCursor(\""+bangla+"\")' value='"+banglaSafe+"' />").appendTo('#vkkars');
		}
	}

	drawButton("2507","vkkars");
	drawButton("2508","vkkars");
	$("<input class='bangla1' type='button' onclick='insertAtCursor(\"\u09CD\u09AF\")' value='\u09CD\u09AF' />").appendTo('#vkkars');

	$("#vknumbers input").attr("class","bangla1");
	$("#vkkars input").attr("class","bangla2");
}
function num2hex(num)
{
	num = new Number(num);
	return num.toString(16);
}

function hex2num(hex)
{
	return parseInt(hex,16);
}

function drawButton(value, container, cls)
{
	//if (cls=="")
	bangla = "\\u0"+num2hex(value);
	banglaSafe = "&#"+value+";";
	$("<input type='button' onclick='insertAtCursor(\""+bangla+"\")' value='"+banglaSafe+"' />").appendTo('#'+container);
}

function drawHexButton(value, container)
{
	$("<input  type='button' onclick='insertAtCursor(\""+value+"\")' value='"+value+"' />").appendTo('#'+container);
}

function hideVk()
{

	//$("#keyb").hide("slow");
	$("#keyb").hide();
}

function showVk(top, left)
{
	$("#keyb").css({"top":top,"left":left,"zIndex":1000});
	//$("#keyb").show("slow");
	$("#keyb").show();
}

function getSelection(activeta)
{
	try{
		var obj=document.getElementById(activeta);
		if(document.selection)
		{
			obj.focus();
			sel=document.selection.createRange();
			return sel.text;
		}
		else if(typeof obj.selectionStart!='undefined')
		{
			var before,after,selection;
			selection=obj.value.substring(obj.selectionStart,obj.selectionEnd);
			return selection;
		}
	}
	catch(e){}
	return"";
}

function bold()
{
	var selection = getSelection();
	insertAtCursor("<b>"+selection+"</b>");
}

function underline()
{
	var selection = getSelection();
	insertAtCursor("<u>"+selection+"</u>");
}

function italic()
{
	var selection = getSelection();
	insertAtCursor("<em>"+selection+"</em>");
}

function h2()
{
	var selection = getSelection();
	insertAtCursor("<h2>"+selection+"</h2>");
}

function link()
{
	var selection = getSelection();
	var link = prompt("url", "url of the link pls");
	insertAtCursor("<a href='"+link+"' target='_blank'>"+selection+"</a>");
}

function link()
{
	var selection = getSelection();
	var link = prompt("<?=$lang->url;?>", "<?=$lang->url;?>");
	insertAtCursor("<a href='"+link+"' target='_blank'>"+selection+"</a>");
}

function youtube()
{
	var link = prompt("Youtube", "Youtube Video ID");
	insertAtCursor("<a href='http://youtube.com/v/"+link+"' /></a>");
}

function mp3()
{
	var link = prompt("Embed MP3 File", "URL of the mp3 file");
	var title = prompt("Embed MP3 File", "File Title");
	insertAtCursor("<a href='"+link+"' />"+title+"</a>");
}

function parseYT() {
    $('a[href^="http://youtube.com"]').flash(
	{ height: 370, width: 450},
	{ version: 8 },
	function(htmlOptions) {
		htmlOptions.src = $(this).attr('href');
		$(this).before("<div style='clear:both; width:450px;height:370px;border:1px solid #000; padding: 0;'>"+$.fn.flash.transform(htmlOptions)+"</div>");
	}
	);
}

function parseMP3()
{
	$('a[href$="mp3"]').flash(
	{ src: BASE+'player/singlemp3player.swf', height: 20, width: 200 },
	{ version: 7 },
	function(htmlOptions) {
		$this = $(this);
		htmlOptions.flashvars.file = $this.attr('href');
		$this.before($.fn.flash.transform(htmlOptions)+"<br/>");
	}
	);


}


function getWindowDimensions()
{
	var myWidth = 0, myHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	var dimensions = new Array(myWidth, myHeight);
	return dimensions;
}

function scrollUp()
{
	var container = document.getElementById('adminmenu');
	container.scrollTop = container.scrollTop - 100;
}

function scrollDown()
{
	var container = document.getElementById('adminmenu');
	container.scrollTop = container.scrollTop + 100;
}

function gmap()
{
	var link = prompt("Location", "Please enter the location (example: Dhanmondi 7, Uttara 12, Gulshan, Gulshan Lake");
	insertAtCursor("<img src='http://prothom-aloblog.com/gmap/gmap.php?location="+link+"' /></img>");
}